'use strict'

const chalk = require('chalk')

const argMap = getArgMap()

function getArgMap() {
  let argvs;
  let res = {};
  try {
    argvs = JSON.parse(process.env.npm_config_argv).original;
  } catch (ex) {
    argvs = process.argv;
  }
  let argv = argvs.slice(2);
  for (let i in argv) {
    let key = argv[i].match(/--(\S*)=/),
      value = argv[i].split('=')[1];
    if (!key || !value) {
      console.log(chalk.red(`  获取命令行参数错误:${argv[i]}`))
    } else {
      res[key[1]] = `"${value}"`
    }
  }
  return res;
};

module.exports = argMap